//
//  View+Theme.swift
//  Exchange
//
//  Created by rui on 2018/4/20.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit


var themePoolKey = "themePool"

struct SelectorKeys {
    public static let background = "setBackgroundColor:"
    public static let buttonTitleColor = "setTitleColor:forState:"
    public static let buttonImage = "setImage:forState:"
    public static let buttonBackgroundImage = "setBackgroundImage:forState:"
}

extension NSObject {
    
    @available(iOS 9.0, *)
    fileprivate var themePool: [Theme:Selector] {
        set {
           objc_setAssociatedObject(self, &themePoolKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get {
            guard let dict =  objc_getAssociatedObject(self, &themePoolKey) else {
                let pool = [Theme:Selector]()
                objc_setAssociatedObject(self, &themePoolKey, pool, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                NotificationCenter.default.removeObserver(self, name: NSNotification.Name.Theme.ThemeNeedChange, object: nil)
                NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheme), name: NSNotification.Name.Theme.ThemeNeedChange, object: nil)
                return pool
            }
            return dict as! [Theme:Selector]
        }
    }
    
    @objc func changeTheme() {
        var themeKey: String
        for obj in self.themePool {
            themeKey = obj.key.themeKey
            if themeKey.hasPrefix("color_") {
                let color = ThemeManager.default.themeColor(themeKey)
                if let themeState = obj.key.themeState {
                    if let btn = self as? UIButton {
                        if obj.value == NSSelectorFromString(SelectorKeys.buttonTitleColor) {
                            btn.setTitleColor(color, for: UIControlState.init(rawValue: themeState))
                        }
                    }
                }else {
                    perform(obj.value, with: color)
                }
            }else if themeKey.hasPrefix("image_") {
                let image = ThemeManager.default.themeImage(themeKey)
                if let themeState = obj.key.themeState {
                    if let btn = self as? UIButton {
                        if obj.value == NSSelectorFromString(SelectorKeys.buttonImage) {
                            btn.setImage(image, for: UIControlState.init(rawValue: themeState))
                        }else if obj.value == NSSelectorFromString(SelectorKeys.buttonBackgroundImage) {
                            btn.setBackgroundImage(image, for: UIControlState.init(rawValue: themeState))
                        }
                    }
                }else {
                    perform(obj.value, with: image)
                }
            }else {
                dPrint("没有该类型主题key")
            }
        }
    }
}

extension UIView {
    @IBInspectable var themeBackground: String {
        get {
            return ""
        }
        set {
           let color = ThemeManager.default.themeColor(newValue)
           let theme = Theme(themeKey: newValue, themeState: nil)
           self.themePool.updateValue(#selector(setter: backgroundColor), forKey: theme)
           backgroundColor = color
        }
    }
    
    @IBInspectable var themeTintColor: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: tintColor), forKey: theme)
            tintColor = color
        }
    }
}

extension UIImageView {
    @IBInspectable var themeImage: String {
        get {
            return ""
        }
        set {
            let img = ThemeManager.default.themeImage(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: image), forKey: theme)
            self.image = img
        }
    }
}


extension UILabel {
    @IBInspectable var themeTextColor: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: textColor), forKey: theme)
            textColor = color
        }
    }
}

extension UIButton {
    func themeSetTitleColor(_ themeColor: String,for state: UIControlState) {
        let color = ThemeManager.default.themeColor(themeColor)
        let theme = Theme(themeKey: themeColor, themeState: state.rawValue)
        self.themePool.updateValue(#selector(setTitleColor(_:for:)), forKey: theme)
        setTitleColor(color, for: state)
    }
    
    func themeSetImage(_ themeImage: String,for state: UIControlState) {
        let image = ThemeManager.default.themeImage(themeImage)
        let theme = Theme(themeKey: themeImage, themeState: state.rawValue)
        self.themePool.updateValue(#selector(setImage(_:for:)), forKey: theme)
        setImage(image, for: state)
    }
    func themeSetBackgroundImage(_ themeImage: String,for state: UIControlState) {
        let image = ThemeManager.default.themeImage(themeImage)
        let theme = Theme(themeKey: themeImage, themeState: state.rawValue)
        self.themePool.updateValue(#selector(setBackgroundImage(_:for:)), forKey: theme)
        setBackgroundImage(image, for: state)
    }
}

extension UITextField {
    
    @IBInspectable var themeTextColor: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: textColor), forKey: theme)
            textColor = color
        }
    }
    
}

extension UITextView {
    
    @IBInspectable var themeTextColor: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: textColor), forKey: theme)
            textColor = color
        }
    }
}

extension UITableView {
    @IBInspectable var themeSeparatorColor: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: separatorColor), forKey: theme)
            separatorColor = color
        }
    }
}

extension CALayer {
    
    var background: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: backgroundColor), forKey: theme)
            backgroundColor = color.cgColor
        }
    }
    var border: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: borderColor), forKey: theme)
            borderColor = color.cgColor
        }
    }
    
    var shadow: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: shadowColor), forKey: theme)
            shadowColor = color.cgColor
        }
    }
    
    override func changeTheme() {
        var themeKey: String
        for obj in self.themePool {
            themeKey = obj.key.themeKey
            if themeKey.hasPrefix("color_") {
                let color = ThemeManager.default.themeColor(themeKey)
                perform(obj.value, with: color.cgColor)
            }else {
                dPrint("没有该类型主题key")
            }
        }
    }
}

extension CAShapeLayer {
    
    var fill: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: fillColor), forKey: theme)
            fillColor = color.cgColor
        }
    }
    var stroke: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: strokeColor), forKey: theme)
            strokeColor = color.cgColor
        }
    }
}

extension UITabBar {
    
    var themeBarTintColor: String {
        get {
            return ""
        }
        set {
            let color = ThemeManager.default.themeColor(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: barTintColor), forKey: theme)
            barTintColor = color
        }
    }
}

extension UIBarItem {
    var themeImage: String {
        get {
            return ""
        }
        set {
            let img = ThemeManager.default.themeImage(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: image), forKey: theme)
            image = img
        }
    }
    
    func themeSetTitleColor(_ themeColor: String,for state: UIControlState) {
        let color = ThemeManager.default.themeColor(themeColor)
        let theme = Theme(themeKey: themeColor, themeState: state.rawValue)
        self.themePool.updateValue(#selector(setTitleTextAttributes(_:for:)), forKey: theme)
        setTitleTextAttributes([.foregroundColor:color], for: state)
    }
    
    override func changeTheme() {
        var themeKey: String
        for obj in self.themePool {
            themeKey = obj.key.themeKey
            if themeKey.hasPrefix("color_") {
                let color = ThemeManager.default.themeColor(themeKey)
                if obj.value == #selector(setTitleTextAttributes(_:for:)) {
                    let state = UIControlState.init(rawValue: obj.key.themeState!)
                    setTitleTextAttributes([.foregroundColor:color], for: state)
                }else {
                    perform(obj.value, with: color)
                }
            }else if themeKey.hasPrefix("image_") {
                let image = ThemeManager.default.themeImage(themeKey)
                perform(obj.value, with: image)
            }else {
                dPrint("没有该类型主题key")
            }
        }
    }
}

extension UITabBarItem {
    
    var themeSelectedImage: String {
        get {
            return ""
        }
        set {
            let img = ThemeManager.default.themeImage(newValue)
            let theme = Theme(themeKey: newValue, themeState: nil)
            self.themePool.updateValue(#selector(setter: selectedImage), forKey: theme)
            selectedImage = img
        }
    }
}



